/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.cstf2.main.Client;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import kn.cstf2.assign.Bean.ListaTablaBean;
import kn.cstf2.assign.Client.MailSender;
import kn.cstf2.assign.Server.AssignServer;
import kn.cstf2.transition.Bean.PersonalBean;
import kn.cstf2.login.Shared.LoginShared;
import kn.cstf2.main.Server.MainServer;
import kn.cstf2.main.Shared.MainShared;
import kn.cstf2.transition.Bean.AsignIfownerStkBean;
import kn.cstf2.transition.Bean.Localidad;
import kn.cstf2.transition.Bean.TransitionBean;
import kn.cstf2.transition.Server.TransitionServer;
import kn.cstf2.transition.Shared.TransitionShared;
import kn.mx.cstf2.dto.UsuarioDto;

/**
 *
 * @author userzaf
 */
public class LoadKnkeysAction extends ActionSupport {

    private String PDF = "pdf";
    private String TRANSITION = "transition";
    private int key;
    private String knkey;
    private Integer knkey2;
    private String username = "";
    private String ClientInfo = "";
    private String CorreoIfOwnerInfo = "";
    private String statusselected = "";
    private String datos = "";
    private String locality;
    private Boolean existe1;
    private Boolean existe2;
    private Boolean existe3;
    private Boolean existe4;
    private Boolean existe5;
    private Boolean existe6;
    private Boolean valBranch;
    private String msjBranch ;
    private InputStream pdfInputStream;
    private String companyname;
    private ArrayList<String> statusList = new ArrayList<String>();
    private ArrayList<TransitionBean> transitionBeanArray;
    private ArrayList<ListaTablaBean> listaTablaBeans;
    private TransitionBean transitionBean = new TransitionBean();
    public static final String ERROR_SESSION = "errorSession";
    private ArrayList<String> personList = new ArrayList<String>();
    private TransitionBean transitionBeanM = new TransitionBean();
    private ArrayList<Localidad> branch = new ArrayList<Localidad>();
    String total;

    

    @Override
    public String execute() throws Exception {
        UsuarioDto usuario = new UsuarioDto();
        AssignServer assignServer = new AssignServer();
        usuario.setName("STK");
        usuario.setPassword("STK");
        LoginShared loginShared = new LoginShared();
        UsuarioDto usr = loginShared.obtenerUsuarioIngreso(usuario);
        System.out.println(usr.getId());
        listaTablaBeans = assignServer.prueba(username);

        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        sesion.put("usuarioSession", usr);
        sesion.put("nombreSTK", username);
        
        return "knkeys";
    }
        
    public String splitData() {
        MainServer ms = new MainServer();
        
        if(companyname != null && knkey2 != null){
            knkey = companyname + ";" + knkey2;
        }
        
        setKey(Integer.parseInt(knkey.split(";")[1]));
        String companyName = (knkey.split(";")[0]);

        TransitionBean transitionBean = new TransitionBean();
        
        transitionBean.setKnkey(key);
        ms.updatedFalse1(key);
        
        
        if(locality != null ){
            MailSender mails = new MailSender();
            TransitionServer ts = new TransitionServer();
            ArrayList<String> correos = new ArrayList<String>();
            
            correos = ts.confirm(locality);
            System.out.println(correos);
            
            StringBuilder mensaje = new StringBuilder();
            mensaje.append("Transition iniciated KNKEY: ");
            mensaje.append(key);
            mensaje.append(" COMPANYNAME: ");
            mensaje.append(companyName);
            
            mails.sendEmail(correos, "Transition", mensaje);
            
            ts.confirmated(key);
            ts.putLocality(key, locality);
            ms.updatedTrue(key);
        }

        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        sesion.put("Knkey", transitionBean);

        if (key != 0) {
            TransitionShared ts = new TransitionShared();
            if (!ts.transitionExists(key)) {
                ts.newTransition(key, companyName);
            }
            loadData(transitionBean);
        }
        return "success";
    }

    private String loadData(TransitionBean transitionBeans) {
        UsuarioDto usuario = new UsuarioDto();
        try {

            statusList.add("On Hold");
            statusList.add("On Going");
            statusList.add("Canceled");
            statusList.add("Closed");
            statusList.add("Completed");

            transitionBeanM = new TransitionBean();
            TransitionShared transitionShared = new TransitionShared();
            TransitionServer transitionServer = new TransitionServer();

            detallesPersonD = transitionShared.GetDataPersonalByKnkey(transitionBeans.getKnkey());
            personList = transitionShared.GetListaNombreDePersonal();

            transitionBeanM = transitionShared.GetTransitionBeanIdKnkey(transitionBeans);
            transitionBeanM.setTime(transitionShared.GetTheDateTime());
            transitionBean = transitionBeanM;

            setClientInfo(transitionShared.GetDataFromSTK(transitionBeanM).getCompanyname() + " - " + transitionShared.GetDataFromSTK(transitionBeanM).getComercialname());
            AsignIfownerStkBean bean = transitionShared.GetIfOwnerFromSTK(transitionBeanM);
            if (bean != null) {
                setCorreoIfOwnerInfo(bean.getIfownwer());
            }

            detallesPersonD = transitionShared.GetDataPersonalByIdPersonal(transitionBean.getId());
            statusselected = transitionShared.GetStatusSelected(transitionBeanM.getKnkey());
            transitionBean.setAcondebtornumbertext(transitionShared.GetJoindocumenttransitionDetail(8, transitionBeanM.getId()));
            transitionBean.setRatetrackertext(transitionShared.GetJoindocumenttransitionDetail(6, transitionBeanM.getId()));

            String TlogDB = transitionShared.GetTransitionLog(transitionBeanM.getKnkey());
            if (TlogDB == null) {
                transitionBeanM.setTlog("");
                transitionShared.UpdateLogEnTransition(transitionBeanM);
                TlogDB = transitionShared.GetTransitionLog(transitionBeanM.getKnkey());

                setDatos(usuario.getName() + "  " + transitionShared.GetTheDateTime() + "  " + TlogDB);
            } else {
                setDatos(TlogDB);
            }
            
            if(transitionServer.verifyUploaded("Customer Profile", key)){
                existe1 = true;
            }
            if(transitionServer.verifyUploaded("rfc", key)){
                existe2 = true;
            }
            if(transitionServer.verifyUploaded("R1", key)){
                existe3 = true;
            }
            if(transitionServer.verifyUploaded("R2", key)){
                existe4 = true;
            }
            if(transitionServer.verifyUploaded("Minimum Selling Rates", key)){
                existe5 = true;
            }
            if(transitionServer.verifyUploaded("Sales Quotation", key)){
                existe6 = true;
            }
                
            branch = transitionServer.branch();
            valBranch = true;
            String msj = transitionServer.getLocality(transitionBeanM.getKnkey());
            if(msj != null)
                msjBranch = transitionServer.validateBranch(transitionBeanM.getKnkey()) + msj;
            
        } catch (Exception ex) {
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }

        if (usuario.getName().trim().equals("STK")) {
            return "knkeylist";
        } else {
            return SUCCESS;
        }
    }

    public Integer getKnkey2() {
        return knkey2;
    }

    public void setKnkey2(Integer knkey2) {
        this.knkey2 = knkey2;
    }

    public String getCompanyname() {
        return companyname;
    }

    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }
    
    public TransitionBean getTransitionBeanM() {
        return transitionBeanM;
    }

    public void setTransitionBeanM(TransitionBean transitionBeanM) {
        this.transitionBeanM = transitionBeanM;
    }

    public ArrayList<String> getPersonList() {
        return personList;
    }

    public void setPersonList(ArrayList<String> personList) {
        this.personList = personList;
    }

    public ArrayList<PersonalBean> getDetallesPersonD() {
        return detallesPersonD;
    }

    public void setDetallesPersonD(ArrayList<PersonalBean> detallesPersonD) {
        this.detallesPersonD = detallesPersonD;
    }
    private ArrayList<PersonalBean> detallesPersonD;

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getKnkey() {
        return knkey;
    }

    public void setKnkey(String knkey) {
        this.knkey = knkey;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        MainShared mainShared = new MainShared();
        setTransitionBeanArray(mainShared.getKnkeysfromClients(username));
    }

    public String getClientInfo() {
        return ClientInfo;
    }

    public void setClientInfo(String ClientInfo) {
        this.ClientInfo = ClientInfo;
    }

    public String getCorreoIfOwnerInfo() {
        return CorreoIfOwnerInfo;
    }

    public void setCorreoIfOwnerInfo(String CorreoIfOwnerInfo) {
        this.CorreoIfOwnerInfo = CorreoIfOwnerInfo;
    }

    public String getStatusselected() {
        return statusselected;
    }

    public void setStatusselected(String statusselected) {
        this.statusselected = statusselected;
    }

    public String getDatos() {
        return datos;
    }

    public String getLocality() {
        return locality;
    }

    public void setLocality(String locality) {
        this.locality = locality;
    }

    public Boolean getExiste1() {
        return existe1;
    }

    public void setExiste1(Boolean existe1) {
        this.existe1 = existe1;
    }

    public Boolean getExiste2() {
        return existe2;
    }

    public void setExiste2(Boolean existe2) {
        this.existe2 = existe2;
    }

    public Boolean getExiste3() {
        return existe3;
    }

    public void setExiste3(Boolean existe3) {
        this.existe3 = existe3;
    }

    public Boolean getExiste4() {
        return existe4;
    }

    public void setExiste4(Boolean existe4) {
        this.existe4 = existe4;
    }

    public Boolean getExiste5() {
        return existe5;
    }

    public void setExiste5(Boolean existe5) {
        this.existe5 = existe5;
    }

    public Boolean getExiste6() {
        return existe6;
    }

    public void setExiste6(Boolean existe6) {
        this.existe6 = existe6;
    }

    public Boolean getValBranch() {
        return valBranch;
    }

    public void setValBranch(Boolean valBranch) {
        this.valBranch = valBranch;
    }

    public String getMsjBranch() {
        return msjBranch;
    }

    public void setMsjBranch(String msjBranch) {
        this.msjBranch = msjBranch;
    }

    public void setDatos(String datos) {
        this.datos = datos;
    }

    public InputStream getPdfInputStream() {
        return pdfInputStream;
    }

    public void setPdfInputStream(InputStream pdfInputStream) {
        this.pdfInputStream = pdfInputStream;
    }

    public ArrayList<String> getStatusList() {
        return statusList;
    }

    public void setStatusList(ArrayList<String> statusList) {
        this.statusList = statusList;
    }

    public ArrayList<TransitionBean> getTransitionBeanArray() {
        return transitionBeanArray;
    }

    public void setTransitionBeanArray(ArrayList<TransitionBean> transitionBeanArray) {
        this.transitionBeanArray = transitionBeanArray;
    }

    public TransitionBean getTransitionBean() {
        return transitionBean;
    }

    public void setTransitionBean(TransitionBean transitionBean) {
        this.transitionBean = transitionBean;
    }

    public ArrayList<Localidad> getBranch() {
        return branch;
    }

    public void setBranch(ArrayList<Localidad> branch) {
        this.branch = branch;
    }

    public LoadKnkeysAction() {
    }

    public ArrayList<ListaTablaBean> getListaTablaBeans() {
        return listaTablaBeans;
    }

    public void setListaTablaBeans(ArrayList<ListaTablaBean> listaTablaBeans) {
        this.listaTablaBeans = listaTablaBeans;
    }
    
}
